package com.ithe.user.controller.app;

import com.ithe.core.result.Result;
import com.ithe.user.pojo.entity.User;
import com.ithe.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by zxd on 2022/5/12
 */
@RequestMapping("/app-api/v1/user")
@Controller
@ResponseBody
@Api(value = "登录相关接口文档")
public class AppUserController {


    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/login")
//    @ApiOperation(value = "登录",notes = "登录",httpMethod = "POST")
//    public Result sendLoginSms(@RequestParam String phone, @RequestParam String code)  {
//        if(!phone.matches("^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$")){
//            return Result.failed(ResultCode.PHONE_ERROR);
//        }
//       //String token=userService.login(phone, code);
//        return Result.success(token);
//    }


//    @PostMapping(value = "/query")
//    @ApiOperation(value = "查询登录信息",notes = "查询登录信息",httpMethod = "POST")
//    public Result query(@RequestParam String token){
//        User user = userService.queryUserByToken(token);
//        return Result.success(user);
//    }
    @PostMapping(value = "/query2")
    @ApiOperation(value = "根据电话获取用户信息",notes = "根据电话获取用户信息",httpMethod = "POST")
    public Result query2(@RequestParam String phone){
        User user = userService.queryUserByToPhone(phone);
        return Result.success(user);
    }

    
//    @ApiOperation(value = "获取用户信息")
//    @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "Long")
//    @GetMapping("/{id}")
//    public Result<UserDTO> getById(@PathVariable Long id) {
//        UserDTO memberDTO = new UserDTO();
//        User user = userService.getOne(
//                new LambdaQueryWrapper<User>()
//                        .select(User::getId, User::getNickName, User::getPhone, User::getBalance)
//                        .eq(User::getId, id)
//        );
//        if (user != null) {
//            BeanUtil.copyProperties(user, memberDTO);
//        }
//        return Result.success(memberDTO);
//    }
//
//    @ApiOperation(value = "获取用户实体信息")
//    @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "Long")
//    @GetMapping("/detail/{id}")
//    public Result<User> getMemberEntityById(
//            @PathVariable Long id
//    ) {
//        User user = userService.getById(id);
//        if (user == null) {
//            return Result.failed(ResultCode.USER_NOT_EXIST);
//        }
//        return Result.success(user);
//    }
//
//
//    @ApiOperation(value = "新增用户")
//    @ApiImplicitParam(name = "user", value = "实体JSON对象", required = true, paramType = "body", dataType = "User")
//    @PostMapping
//    public Result<Long> add(@RequestBody User user) {
//        boolean status = userService.save(user);
//        if (status) {
//            return Result.success(user.getId());
//        } else {
//            return Result.failed();
//        }
//    }
//
//    @ApiOperation(value = "修改用户")
//    @PutMapping("/{id}")
//    public <T> Result<T> add(@PathVariable Long id, @RequestBody User user) {
//        boolean status = userService.updateById(user);
//        return Result.judge(status);
//    }
//
//    @ApiOperation(value = "获取登录用户信息")
//    @GetMapping("/me")
//    public Result<UserDTO> getUserInfo() {
//        User user = userService.getById(UserThreadLocal.get().getId());
//        if (user == null) {
//            return Result.failed(ResultCode.USER_NOT_EXIST);
//        }
//        UserDTO userDTO = new UserDTO();
//        BeanUtil.copyProperties(user, userDTO);
//        return Result.success(userDTO);
//    }
//
//
//    @ApiOperation(value = "修改用户积分")
//    @PutMapping("/{id}/points")
//    public <T> Result<T> updatePoint(@PathVariable Long id, @RequestParam Integer num) {
//        User user = userService.getById(id);
//        user.setPoint(user.getPoint() + num);
//        boolean result = userService.updateById(user);
//        return Result.judge(result);
//    }
//
//    @ApiOperation(value = "扣减用户余额")
//    @PutMapping("/current/balances/_deduct")
//    public <T> Result<T> deductBalance(@RequestParam Long balances) {
//        boolean result = userService.update(new LambdaUpdateWrapper<User>()
//                .setSql("balance = balance - " + balances)
//                .eq(User::getId, UserThreadLocal.get())
//        );
//        return Result.judge(result);
//    }
//
//    @ApiOperation(value = "添加浏览历史")
//    @PostMapping("/view/history")
//    public <T> Result<T> addProductViewHistory(@RequestBody ProductHistoryVO product) {
//        userService.addProductViewHistory(product, UserThreadLocal.get().getId());
//        return Result.success();
//    }
//
//    @ApiOperation(value = "获取浏览历史")
//    @GetMapping("/view/history")
//    public Result<Set<ProductHistoryVO>> getProductViewHistory() {
//        try {
//            Set<ProductHistoryVO> historyList = userService.getProductViewHistory(UserThreadLocal.get().getId());
//            return Result.success(historyList);
//        } catch (Exception e) {
//            return Result.success(Collections.emptySet());
//        }
//    }



}
